package com.lswms.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.lswms.common.exception.ErrorCode;
import com.lswms.common.exception.RenException;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.ConvertUtils;
import com.lswms.common.utils.ZqDateUtil;
import com.lswms.dao.YnDcProdDao;
import com.lswms.dto.YnDcProdDTO;
import com.lswms.dto.vo.req.DcConfirmUserInfoReq;
import com.lswms.dto.vo.req.DcDataDetailReq;
import com.lswms.dto.vo.req.DcIndexDataReq;
import com.lswms.dto.vo.resp.DcDataDetailResp;
import com.lswms.dto.vo.resp.DcDataInfoResp;
import com.lswms.dto.vo.resp.DcIndexDataResp;
import com.lswms.entity.YnDcLoginEntity;
import com.lswms.entity.YnDcProdClickEntity;
import com.lswms.entity.YnDcProdEntity;
import com.lswms.entity.YnDcUserEntity;
import com.lswms.entity.YnUserEntity;
import com.lswms.service.SysConfigService;
import com.lswms.service.YnDcProdClickService;
import com.lswms.service.YnDcProdService;
import com.lswms.service.YnDcUserService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import lombok.extern.slf4j.Slf4j;

/**
 * 产品表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-07-24
 */
@Service
@Slf4j
public class YnDcProdServiceImpl extends CrudServiceImpl<YnDcProdDao, YnDcProdEntity, YnDcProdDTO> implements YnDcProdService {

	@Autowired
	private SysConfigService sysConfigService;
	
	@Autowired
	private YnDcUserService ynDcUserService;
	
	@Autowired
	private YnDcProdClickService ynDcProdClickService;

	//信用商品
	private static final String SEND_PROD = "Send_Prod";

	
    @Override
    public QueryWrapper<YnDcProdEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<YnDcProdEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 首页数据
     */
	@Override
	public List<DcIndexDataResp> dcIndexData(DcIndexDataReq dto) {
		log.info("首页数据，参数:{}",JSON.toJSONString(dto));
		//返回结构
		List<DcIndexDataResp> list = new ArrayList<DcIndexDataResp>();
		if("1".equals(dto.getType())){
			//推荐商品
			QueryWrapper<YnDcProdEntity> wrapper = new QueryWrapper<YnDcProdEntity>();
			wrapper.eq("status", 1);
			wrapper.eq("recommend", 1);
			List<YnDcProdEntity> prod = baseDao.selectList(wrapper);
			if(prod != null && prod.size() > 0){
				int a = ThreadLocalRandom.current().nextInt(0, prod.size() - 1);
				YnDcProdEntity prodEntity = prod.get(a);
				
				DcIndexDataResp resp1 = new DcIndexDataResp();
				resp1.setTitle(prodEntity.getTitle());
				resp1.setSubTitle(prodEntity.getRemarks());
				resp1.setMyLimitPosition(prodEntity.getMoney());
				resp1.setId(prodEntity.getId()+"");
				resp1.setDesc("日利率"+prodEntity.getRate()+"，最高额度"+prodEntity.getMoney());
				list.add(resp1);
			}
		}
		//信用商品
		String value = sysConfigService.queryConfig(SEND_PROD);
		if(!StringUtils.isBlank(value)){
			 DcIndexDataResp resp1 = new DcIndexDataResp();
			 resp1.setTitle(value.split("#")[0]);
			 resp1.setSubTitle(value.split("#")[1]);
			 resp1.setMyLimitPosition(value.split("#")[2]);
			 resp1.setId("0");
			 resp1.setDesc(value.split("#")[3]);
			 list.add(resp1);
		}
		log.info("首页数据，返回:{}",JSON.toJSONString(list));
		return list;
	}
	/**
	 * 转换对象
	 * @param prodEntity
	 * @return
	 */
	@Override
	public DcDataInfoResp convetInfo(YnDcProdEntity prodEntity){
		DcDataInfoResp resp1 = new DcDataInfoResp();
		resp1.setTitle(prodEntity.getTitle());
		resp1.setSubTitle(prodEntity.getRemarks());
		resp1.setCount(prodEntity.getCount());
		resp1.setLogo(prodEntity.getLogo());
		resp1.setMoney(prodEntity.getMoney());
		resp1.setParam(prodEntity.getParam());
		resp1.setRate(prodEntity.getRate());
		resp1.setSuccess_rate(prodEntity.getSuccessRate());
		resp1.setTerm(prodEntity.getTerm());
		resp1.setId(prodEntity.getId()+"");
		resp1.setSend(prodEntity.getSend());
		return resp1;
	}
	/**
	 * 产品列表
	 */
	@Override
	public List<DcDataInfoResp> dcDataList() {
		log.info("产品列表");
		//返回结构
		List<DcDataInfoResp> list = new ArrayList<DcDataInfoResp>();
		//产品列表
		QueryWrapper<YnDcProdEntity> wrapper = new QueryWrapper<YnDcProdEntity>();
		wrapper.eq("status", 1);
		wrapper.orderByDesc("create_date");
		List<YnDcProdEntity> prod = baseDao.selectList(wrapper);
		if(prod != null && prod.size() > 0){
			for(YnDcProdEntity prodEntity : prod){
				DcDataInfoResp resp1 = convetInfo(prodEntity);
				list.add(resp1);
			}
		}
		log.info("产品列表，返回:{}",JSON.toJSONString(list));
		return list;
	}
	/**
	 * 产品详情
	 */
	@Override
	public DcDataDetailResp dcDataDetail(DcDataDetailReq dto) {
		log.info("产品详情，参数:{}",JSON.toJSONString(dto));
		YnDcProdEntity prod = baseDao.selectById(Long.parseLong(dto.getId()));
		DcDataInfoResp resp1 = convetInfo(prod);
		DcDataDetailResp resp = ConvertUtils.sourceToTarget(resp1, DcDataDetailResp.class);
		//最新借款信息
		List<String> notice = new ArrayList<String>();
		notice.add("恭喜Marx成功获得放款");
		notice.add("恭喜Uls成功获得放款");
		notice.add("恭喜Charry成功获得放款");
		notice.add("恭喜Joe成功获得放款");
		notice.add("恭喜Ttue成功获得放款");
		resp.setNotice(notice);
		//最近5小时借款人数
		List<Map<String,String>> rank = new ArrayList<Map<String,String>>();
		Calendar calendar = Calendar.getInstance();
		int curHour24 = calendar.get(calendar.HOUR_OF_DAY);
		for(int a = 0;a<=4;a++){
			int b = curHour24 - a;
			String c = b+"";
			if(b < 10){
				c = "0"+b;
			}
			Map<String,String> map = new HashMap<String,String>();
			int random = ThreadLocalRandom.current().nextInt(80, 100);
			map.put("count", random+"");
			map.put("time", c+":00");
		}
		resp.setRank(rank);
		log.info("产品详情，返回:{}",JSON.toJSONString(resp));
		return resp;
	}

	/**
	 * 产品详情提交用户信息
	 */
	@Override
	public String dcConfirmUserInfo(DcConfirmUserInfoReq dto,YnUserEntity ynuser) {
		log.info("产品详情提交用户信息，参数:{}",JSON.toJSONString(dto));
		YnDcProdEntity prod = baseDao.selectById(Long.parseLong(dto.getId()));
		if(prod == null){
			throw new RenException(ErrorCode.DC_PROD_IS_NOT_EXIST);
		}
		//添加用户信息
		YnDcUserEntity entity = new YnDcUserEntity();
		entity.setCardno(dto.getCardno());
		entity.setCreateDate(ZqDateUtil.getNow());
		entity.setName(dto.getName());
		entity.setUsername(dto.getMobile());
		entity.setPid(prod.getId());
		entity.setPname(prod.getTitle());
		entity.setSex(Integer.parseInt(dto.getGender()));
		entity.setUserid(ynuser.getId());
		ynDcUserService.insert(entity);
		//添加产品点击记录
		YnDcProdClickEntity click = new YnDcProdClickEntity();
		click.setCreateDate(ZqDateUtil.getNow());
		click.setPid(prod.getId());
		click.setPname(prod.getTitle());
		click.setUid(ynuser.getId());
		ynDcProdClickService.insert(click);
		
		return prod.getUrl();
	}
	/**
	 * 获取一个推荐产品
	 */
	@Override
	public YnDcProdEntity getRecommendProd() {
		//推荐商品
		YnDcProdEntity prodEntity = new YnDcProdEntity();
		
		QueryWrapper<YnDcProdEntity> wrapper = new QueryWrapper<YnDcProdEntity>();
		wrapper.eq("status", 1);
		wrapper.eq("recommend", 1);
		List<YnDcProdEntity> prod = baseDao.selectList(wrapper);
		if(prod != null && prod.size() > 0){
			int a = ThreadLocalRandom.current().nextInt(0, prod.size() - 1);
			prodEntity = prod.get(a);
		}
		log.info("获取一个推荐产品,返回:{}",JSON.toJSONString(prodEntity));
		return prodEntity;
	}


}